# byrow 为 TRUE 元素按行排列
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)

# Ebyrow 为 FALSE 元素按列排列
N <- matrix(c(3:14), nrow = 4, byrow = FALSE)

# 定义行和列的名称
rownames =c("row1", "row2", "row3", "row4")

colnames = c("col1", "col2", "col3")
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))

# 创建 2 行 3 列的矩阵
matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2)
print(matrix1)

matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2)
print(matrix2)

# 两个矩阵相加
result <- matrix1 + matrix2
cat("相加结果：","\n")
print(result)

# 两个矩阵相减
result <- matrix1 - matrix2
cat("相减结果：","\n")
print(result)


# 创建 2 行 3 列的矩阵
matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2)
print(matrix1)

matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2)
print(matrix2)

# 两个矩阵相乘
result <- matrix1 * matrix2
cat("相乘结果：","\n")
print(result)

# 两个矩阵相除
result <- matrix1 / matrix2
cat("相除结果：","\n")
print(result)


# 创建一个3x3的矩阵
my_matrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3)
print(my_matrix)

# 创建一个包含矩阵和向量的列表
my_list <- list(matrix(c(1, 2, 3, 4), nrow = 2), c(5, 6, 7))
print(my_list)


my_vector <- c(1, 2, 3, 4)
my_array <- array(my_vector, dim = c(2,2))
print(my_array)


dim1 <- c("A1","A2")
dim2 <- c("B1","B2","B3")
dim3 <- c("C1","C2","C3","C4")

z <- array(1:24,c(2,3,4),dimnames = list(dim1,dim2,dim3))





table = data.frame(
  姓名 = c("张三", "李四"),
  工号 = c("001","002"),
  月薪 = c(1000, 2000)
)

print(table) # 查看 table 数据

str(table)

print(summary(table))


result <- data.frame(table$姓名,table$月薪)
print(result)

result <- table[,"工号"]
print(result)

table = data.frame(
  姓名 = c("张三", "李四","王五"),
  工号 = c("001","002","003"),
  月薪 = c(1000, 2000,3000)
)

print(table)

# 读取第 2 、3 行的第 1 、2 列数据：
result <- table[c(2,3),c(1,2)]
print(result)

table = data.frame(
  姓名 = c("张三", "李四","王五"),
  工号 = c("001","002","003"),
  月薪 = c(1000, 2000,3000)
)
# 添加部门列
table $ 部门 <- c("运营","技术","编辑")

print(table)

# 创建向量
sites <- c("Google","Runoob","Taobao")
likes <- c(222,111,123)
url <- c("www.google.com","www.runoob.com","www.taobao.com")

# 将向量组合成数据框
addresses <- cbind(sites,likes,url)

# 查看数据框
print(addresses)



table = data.frame(
  姓名 = c("张三", "李四","王五"),
  工号 = c("001","002","003"),
  月薪 = c(1000, 2000,3000)
)
newtable = data.frame(
  姓名 = c("小明", "小白"),
  工号 = c("101","102"),
  月薪 = c(5000, 7000)
)
# 合并两个数据框
result <- rbind(table,newtable)
print(result)



x <- c("男", "女", "男", "男",  "女")
sex <- factor(x)
print(sex)
print(is.factor(sex))





x <- c("男", "女", "男", "男",  "女",levels=c('男','女'))
sex <- factor(x)
print(sex)
print(is.factor(sex))


patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)

print(patientdata)

#第三行的全部数据
patientdata[3, ]

#3号病人的age
patientdata[patientdata$patientID == 3, "age"]

#所有病人的age列数据
patientdata$age

#1、4号病人的age和status
patientdata[patientdata$patientID %in% c(1, 4), c("age", "status")]

#2-4号病人的status
patientdata[patientdata$patientID >= 2 & patientdata$patientID <= 4, "status"]

patientdata[patientdata$patientID %in% c(2:4), "status"]


a <- list(姓名=c('张三', '李四'), 性别=c('男', '女'), 工资=c(1800, 2800))
a$姓名 <- c('赵六', '王五')
a$性别 <- c('male', 'female')

